#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<queue>

using namespace std;

vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
    if (n == 1)  return { 0 };
    vector<vector<int>> g(n);
    vector<int> in(n);
    for (auto& e : edges)
    {
        in[e[0]]++;
        in[e[1]]++;
        g[e[0]].push_back(e[1]);
        g[e[1]].push_back(e[0]);
    }

    queue<int> q;
    for (int i = 0; i < n; i++)
        if (in[i] == 1)
            q.push(i);

    vector<int> res;
    while (q.size())
    {
        res.clear();
        int sz = q.size();
        for (int i = 0; i < sz; i++)
        {
            int t = q.front();
            q.pop();
            res.push_back(t);
            for (auto next : g[t])
                if (--in[next] == 1)
                    q.push(next);
        }
    }
    return res;
}

int main()
{
    vector<vector<int>> vv = { {1,0},{1,2},{1,3} };
    findMinHeightTrees(4, vv);
    return 0;
}